//
// Created by sgyt on 2020/5/7.
//

#ifndef LINUXCORE_BYTE_LOCK_H
#define LINUXCORE_BYTE_LOCK_H
#define writew_lock(fd,offset,where,len) \
                lock_reg((fd),F_SETLKW,F_WRLCK,(offset),(where),(len))

pid_t lock_test(int fd,int type,off_t offset,int where,off_t len);
int lock_reg(int fd,int cmd,int type,off_t offset,int where,off_t len);



#define read_lock(fd,offset,where,len) \
            lock_reg((fd),F_SETLK,F_RDLCK,(offset),(where),(len))

#define readw_lock(fd,offset,where,len) \
            lock_reg((fd),F_SETLKW,F_RDLCK,(offset),(where),(len))

#define write_lock(fd,offset,where,len) \
            lock_reg((fd),F_SETLK,F_WRLCK,(offset),(where),(len))

#define un_lock(fd,offset,where,len) \
            lock_reg((fd),F_SETLK,F_UNLCK,(offset),(where),(len))

#define is_read_lockable(fd,offset,where,len) \
            (lock_test((fd),F_RDLCK,(offset),(where),(len)) == 0)

#define is_write_lockable(fd,offset,where,len) (lock_test((fd),F_WRLCK,(offset),(where),(len)) == 0)

#endif //LINUXCORE_BYTE_LOCK_H
